package Test;

import Bean.Worker;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by DespairK on 2016/10/8.
 */
public class ReadExcel {

    private int nameInNum = -1;
    private int dataInNum = -1;
    //    private int signTimeInNum = -1;
//    private int returnTimeInNum = -1;
    private int nameInNum2 = -1;
    private int dataInNum2 = -1;
    private int morningInNum2 = -1;
    private int afternoonInNum2 = -1;
    private int eveningInNum2 = -1;
    private int teshuInNum2 = -1;
    private List<Worker> workList = new ArrayList<>();

    public List<Worker> testReadExcel() throws IOException {
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("1.xls"));
        HSSFWorkbook wb = (HSSFWorkbook) WorkbookFactory.create(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        for (int i = 0; i <= sheet.getLastRowNum(); i++) {
            HSSFRow row = sheet.getRow(i);
            Worker worker = new Worker();
            for (int j = row.getFirstCellNum(); j < (int) row.getLastCellNum(); j++) {
                HSSFCell cell = row.getCell(j);
                cell.setCellType(Cell.CELL_TYPE_STRING);
                if (i == 0) {
                    if (cell.getStringCellValue().equals("姓名")) {
                        nameInNum = j;
                    } else if (cell.getStringCellValue().equals("出勤时间")) {
                        dataInNum = j;
                    }
                } else {
                    if (j == nameInNum) {
                        worker.setName(cell.getStringCellValue());
                    } else if (j == dataInNum) {
                        worker.setData(cell.getStringCellValue());
                    }
                    processingData(row);
                }
            }
            if (i != 0 && worker != null) {
                workList.add(worker);
            }
        }
        return workList;
    }

    public List<Worker> testReadExce2() throws IOException {
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("2.xls"));
        HSSFWorkbook wb = (HSSFWorkbook) WorkbookFactory.create(fs);
        HSSFSheet sheet = wb.getSheetAt(0);
        for (int i = 0; i <= sheet.getLastRowNum(); i++) {
            HSSFRow row = sheet.getRow(i);
            Worker worker = new Worker();
            for (int j = row.getFirstCellNum(); j < (int) row.getLastCellNum(); j++) {
                HSSFCell cell = row.getCell(j);
                if (cell != null) {
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                }
                if (i == 0) {
                    if (cell.getStringCellValue().equals("姓名")) {
                        nameInNum2 = j;
                    } else if (cell.getStringCellValue().equals("日期")) {
                        dataInNum2 = j;
                    } else if (cell.getStringCellValue().equals("上午")) {
                        morningInNum2 = j;
                    } else if (cell.getStringCellValue().equals("下午")) {
                        afternoonInNum2 = j;
                    } else if (cell.getStringCellValue().equals("晚上")) {
                        eveningInNum2 = j;
                    } else if (cell.getStringCellValue().equals("特殊")) {
                        teshuInNum2 = j;
                    }
                } else {
                    if (j == nameInNum2) {
                        worker.setName(cell != null ? cell.getStringCellValue() : "");
                    } else if (j == dataInNum2) {
                        worker.setData(cell != null ? cell.getStringCellValue() : "0");
                    } else if (j == morningInNum2) {
                        worker.setMorningHour(cell != null ? cell.getStringCellValue() : "0");
                    } else if (j == afternoonInNum2) {
                        worker.setAfternoonHour(cell != null ? cell.getStringCellValue() : "0");
                    } else if (j == eveningInNum2) {
                        worker.setEveningHour(cell != null ? cell.getStringCellValue() : "0");
                    } else if (j == teshuInNum2) {
                        worker.setTeshu(cell != null ? cell.getStringCellValue() : "0");
                    }
//                    processingData(row);
                }
            }
            if (i != 0 && worker != null) {
                workList.add(worker);
            }
        }
        return workList;
    }

    private void processingData(HSSFRow row) {
        for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
            HSSFCell cell = row.getCell(i);
            if (cell != null) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
            }
        }
    }

}
